<template>
  <div>
    <!-- 面包屑 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>角色管理</el-breadcrumb-item>
      <el-breadcrumb-item>角色列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片 -->
    <el-card class="box-card">
      <el-table :data="RoleList" style="width: 100%">
        <el-table-column type="expand">
          <!-- 每个权级占一行 -->
          <template slot-scope="props">
            <el-row
              v-for="(item1,index1) in props.row.children"
              :key="item1.id"
              :class="['bdbotton',index1===0?'bdtop':'','optimize']"
            >
              <el-col :span="5">
                <el-tag>{{item1.authName}}</el-tag>
                <i class="el-icon-caret-right"></i>
              </el-col>
              <el-col :span="19">
                <el-row v-for="(item2, index1) in item1.children" :key="index1" class="optimize">
                  <el-col :span="6">
                    <el-tag type="success">
                      {{item2.authName}}
                      <i class="el-icon-caret-right"></i>
                    </el-tag>
                  </el-col>
                  <el-col :span="18">
                    <el-tag
                      type="warning"
                      closable
                      @close="removeRoleById(props.row,item3.id)"
                      v-for="(item3, index3) in item2.children"
                      :key="index3"
                    >{{item2.authName}}</el-tag>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-table-column>
        <el-table-column label="#" type="index"></el-table-column>
        <el-table-column label="角色名称" prop="roleName"></el-table-column>
        <el-table-column label="角色描述" prop="roleDesc"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scoped">
            <el-button type="primary" size="mini" icon="el-icon-edit">编辑</el-button>
          <el-button type="danger" size="mini" icon="el-icon-delete">删除</el-button>
          <el-button type="warning" size="mini" icon="el-icon-delete" @click="roleUserList(scoped.row)">分配角色</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 权限弹出框 -->
      <el-dialog title="提示" :visible.sync="roleDialogVisible" width="50%" @close="getRoleClose">
        <el-tree :data="roleDialogList" :props="roleTerrProps" node-key="id" :default-checked-keys="checkoutKeys" default-expand-all show-checkbox ref="treeRef"></el-tree>

        <span slot="footer" class="dialog-footer">
          <el-button @click="roleDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="roleDialog">确 定</el-button>
        </span>
      </el-dialog>
    </el-card>
  </div>
</template>
<script>
export default {
  data () {
    return {
      RoleList: [],
      // 权限弹出框是否弹出
      roleDialogVisible: false,
      // 权限弹出框数据列表
      roleDialogList: [],
      // 树形控件的配置代码
      roleTerrProps: {
        children: 'children',
        label: 'authName'
      },
      checkoutKeys: [],
      // 保存id值
      roleId: ''
    }
  },
  created () {
    this.getRoleList()
  },
  methods: {
    async getRoleList () {
      const { data: res } = await this.$http.get('roles')
      console.log(res)
      this.RoleList = res.data
      console.log(res.data)
    },
    removeRoleById (role, id) {
      this.$confirm('确定删除吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async (item) => {
        console.log(item)
        if (item === 'confirm') {
          const { data: res } = await this.$http.delete(
            `roles/${role.id}/rights/${id}`
          )
          console.log(res)
          if (res.meta.status !== 200) { return this.$message.error('删除权限失败') }
          role.children = res.data
          this.$message({
            message: '删除权限成功',
            type: 'success'
          })
        }
      })
    },
    async roleUserList (item) {
      const { data: res } = await this.$http.get('rights/tree ')
      this.roleDialogList = res.data
      console.log(res)
      // 获取三级id
      this.getLeafKeys(item)
      // 赋值id
      this.roleId = item.id
      this.roleDialogVisible = true
    },
    // 获取三级id
    getLeafKeys (item) {
      if (!item.children) {
        this.checkoutKeys.push(item.id)
      } else {
        item.children.forEach(subitem => {
          this.getLeafKeys(subitem)
        })
      }
    },
    // 监听
    getRoleClose () {
      this.checkoutKeys = []
    },
    async roleDialog () {
      const rids = [
        ...this.$refs.treeRef.getCheckedKeys(),
        ...this.$refs.treeRef.getHalfCheckedKeys()
      ]
      await this.$http.post('roles/' + this.roleId + '/rights', { rids: rids.join(',') })
      // 关闭对话框
      this.roleDialogVisible = false
      // 调用数据
      this.getRoleList()
    }
  }
}
</script>
<style lang="less" scoped>
.el-tag {
  margin: 7px;
}
.bdtop {
  border-top: 1px solid #ccc;
}
.bdbotton {
  border-bottom: 1px solid #ccc;
  border-left: 1px solid #ccc;
  border-right: 1px solid #ccc;
}
.optimize {
  display: flex;
  align-items: center;
}
</style>
